package com.codeczx.datademo;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.create)
    Button create;
    @BindView(R.id.button2)
    Button upgrade;
    @BindView(R.id.add)
    Button add;
    @BindView(R.id.update)
    Button update;
    @BindView(R.id.query)
    Button query;
    @BindView(R.id.delete)
    Button delete;

    private MyDataBaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        dbHelper = new MyDataBaseHelper(this,"BookStore.db",null,2);
    }

    @OnClick({R.id.create, R.id.button2, R.id.add, R.id.update, R.id.query, R.id.delete})
    public void onViewClicked(View view) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        switch (view.getId()) {
            case R.id.create:
                dbHelper.getWritableDatabase();
                break;
            case R.id.button2:
                break;
            case R.id.add:
                values.put("name","codeczx daily");
                values.put("author","czx");
                values.put("pages",10);
                values.put("price",16.96);
                db.insert("Book",null,values);
                values.clear();
                break;
            case R.id.update:
                values.put("price",100);
                db.update("Book",values,"author = ?",new String[]{"czx"});
                values.clear();
                break;
            case R.id.query:
                Cursor cursor = db.query("Book",null,null,null,null,null,null);
                if (cursor.moveToFirst()) {
                    do{
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.i(TAG, "onViewClicked: name"+name);
                        Log.i(TAG, "onViewClicked: author"+author);
                        Log.i(TAG, "onViewClicked: pages"+pages);
                        Log.i(TAG, "onViewClicked: price"+price);
                    }while(cursor.moveToNext());
                }
                cursor.close();
                break;
            case R.id.delete:
                db.delete("Book","price > ?",new String[]{"50"});
                break;
        }
    }
}
